package com.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.domain.Goods;
import com.util.JdbcUtils;

public class daoGoods {

	/**
	 * 查询种类 方法，返回List集合  对应数据库 goods_sorts
	 * 
	 * @param sorts
	 * @param len
	 * @return
	 */
	public List<Goods> selsectSORTS(String sorts, int len) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		
		//执行数据库查询
		String sql = "SELECT * FROM goods WHERE goods.goods_sorts = ? ORDER BY goods.goods_id DESC LIMIT ?";
		try {
			List<Goods> query = queryRunner.query(sql, new BeanListHandler<>(Goods.class), new Object[] { sorts, len });
			return query;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 以Goods Bean中  ID 查询商品
	 * @param goods
	 * @return Goods 查询到的数据
	 */
	public Goods selectGoodsInfo(Goods goods) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		
		//执行数据库查询
		String sql = "SELECT * FROM goods WHERE goods.goods_id = ?";
		try {
			Goods query = queryRunner.query(sql, new BeanHandler<>(Goods.class), goods.getGoods_id());
			return query;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 传递String 类型值 查找 查询商品信息 返回这个商品信息
	 * @param goods
	 * @return Goods 返回这个商品信息
	 */
	public Goods selectGoodsInfo(String goods) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		
		//执行数据库查询
		String sql = "SELECT * FROM goods WHERE goods.goods_id = ?";
		try {
			Goods query = queryRunner.query(sql, new BeanHandler<>(Goods.class), goods);
			return query;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}


}
